<?php
namespace app\api\controller\member;
use app\common\controller\BaseController;
use app\common\service\api\AlibabaSdkService;
use app\common\service\file\FileService;
use app\common\service\member\MemberIdCardService;
use app\common\service\member\MemberService;
use app\common\service\member\SettingService as MemberSetting;
use app\common\validate\file\FileValidate;
use app\common\validate\member\MemberIdCardValidate;
use app\common\validate\member\MemberValidate;
use hg\apidoc\annotation as Apidoc;
/**
 * @Apidoc\Title("用户身份证信息")
 * @Apidoc\Group("member")
 * @Apidoc\Sort("250")
 */
class MemberIdCard extends BaseController
{
    /**
    * @Apidoc\Title("用户身份证信息列表")
    * @Apidoc\Query(ref="pagingQuery")
    * @Apidoc\Query(ref="sortQuery")
    * @Apidoc\Query(ref="searchQuery")
    * @Apidoc\Query(ref="dateQuery")
    * @Apidoc\Returned(ref="expsReturn")
    * @Apidoc\Returned(ref="pagingReturn")
    * @Apidoc\Returned("list", type="array", desc="用户身份证信息列表", children={
    *   @Apidoc\Returned(ref="app\common\model\member\MemberIdCardModel", field="id,is_disable,create_time,update_time,member_id,card_front_id,card_opposite_id,auth_status,auth_msg,auth_time,auth_uid,name,nationality,num,sex,birth,is_face_fake,address,start_date,end_date,issue,is_back_fake,card_hand_id")
    * })
    */
    public function list()
    {
        $where = $this->where(where_delete());
        $data = MemberIdCardService::list($where, $this->page(), $this->limit(), $this->order());
        return success($data);
    }
    /**
    * @Apidoc\Title("用户身份证信息信息")
    * @Apidoc\Query(ref="app\common\model\member\MemberIdCardModel", field="id")
    * @Apidoc\Returned(ref="app\common\model\member\MemberIdCardModel")
    */
    public function info()
    {
        $param['member_id'] = member_id(true);
        $data = MemberIdCardService::info($param['member_id'],false);
        return success($data);
    }
    /**
    * @Apidoc\Title("用户身份证信息添加")
    * @Apidoc\Method("POST")
    * @Apidoc\Param(ref="app\common\model\member\MemberIdCardModel", field="is_disable,member_id,card_front_id,card_opposite_id,auth_status,auth_msg,auth_time,auth_uid,name,nationality,num,sex,birth,is_face_fake,address,start_date,end_date,issue,is_back_fake,card_hand_id")
    */
    public function add()
    {
        $param = $this->params(MemberIdCardService::$edit_field);
        validate(MemberIdCardValidate::class)->scene('add')->check($param);
        $data = MemberIdCardService::add($param);
        return success($data);
    }
    /**
    * @Apidoc\Title("用户身份证信息保存")
    * @Apidoc\Method("POST")
    * @Apidoc\Param(ref="app\common\model\member\MemberIdCardModel", field="id,is_disable,member_id,card_front_id,card_opposite_id,auth_status,auth_msg,auth_time,auth_uid,name,nationality,num,sex,birth,is_face_fake,address,start_date,end_date,issue,is_back_fake,card_hand_id")
    */
    public function save()
    {
        $param = $this->params(MemberIdCardService::$edit_field);
        $param['member_id'] = member_id(true);
        validate(MemberIdCardValidate::class)->scene('edit')->check($param);
        $memberSetting = MemberSetting::info('real_name_auth,is_auto_auth,is_idcard_recognize,is_idcard_auth,is_upload_idcard_front,is_upload_idcard_opposite,is_upload_idcard_hand,is_bank_card_recognize');
        if($memberSetting['is_upload_idcard_front']==1 && !$param['card_front_id']){
            exception("请上传身份证正面");
        }
        if($memberSetting['is_upload_idcard_opposite']==1 && !$param['card_opposite_id']){
            exception("请上传身份证反面");
        }
        if($memberSetting['is_upload_idcard_hand']==1 && !$param['card_hand_id']){
            exception("请上传手持身份证");
        }
        //身份证认证三要素
        if($memberSetting['is_idcard_auth']==1){
            $memberInfo = MemberService::getUserInfo($param['member_id'], true, 'phone');
            $ali_result = AlibabaSdkService::threeElements($param['name'],$param['num'],$memberInfo['phone']);
            if($ali_result['code']=='400'){
                exception($ali_result['msg']);
            }else if($ali_result['code']=='200') {
                $param['is_3ys_auth'] =1;
                //判断是否自动通过审核
                if($memberSetting['is_auto_auth']==1){
                    $param['auth_status'] =2;
                    $param['auth_msg'] ='根据三要素接口自动通过审核';
                    $param['auth_time'] =datetime();
                    //修改用户信息
                    $member_data=['is_authentication'=>1];
                    if(isset($param['name']) && $param['name']){
                        $member_data['name'] =$param['name'];
                    }
                    if(isset($param['sex']) && $param['sex']){
                        $member_data['gender'] =$param['sex']=='男'?1:($param['sex']=='女'?2:0);
                    }
                    $member_edit = MemberService::edit($param['member_id'],$member_data);
                }
            }
        }

        $data = MemberIdCardService::save($param['member_id'], $param);
        return success($data);
    }
    /**
    * @Apidoc\Title("用户身份证信息删除")
    * @Apidoc\Method("POST")
    * @Apidoc\Param(ref="idsParam")
    */
    public function dele()
    {
        $param = $this->params(['ids/a' => []]);
        validate(MemberIdCardValidate::class)->scene('dele')->check($param);
        $data = MemberIdCardService::dele($param['ids']);
        return success($data);
    }
    /**
     * @Apidoc\Title("用户身份证信息禁用")
     * @Apidoc\Method("POST")
     * @Apidoc\Param(ref="idsParam")
     * @Apidoc\Param(ref="app\common\model\member\MemberIdCardModel", field="is_disable")
     */
    public function disable()
    {
        $param = $this->params(['ids/a' => [], 'is_disable/d' => 0]);
        validate(MemberIdCardValidate::class)->scene('disable')->check($param);
        $data = MemberIdCardService::edit($param['ids'], $param);
        return success($data);
    }
    /**
     * @Apidoc\Title("上传身份证")
     * @Apidoc\Method("POST")
     * @Apidoc\ParamType("formdata")
     * @Apidoc\Param(ref="fileParam")
     * @Apidoc\Returned(ref="fileReturn")
     */
    public function uploadIdCard()
    {
        $param = $this->params([
            'side/s'  => '',
            'file_type/s' => 'image',
            'file_name/s' => '',
        ]);
        $param['member_id'] = member_id(true);
        $param['file']      = $this->request->file('file');
        $param['is_front']  = 1;
        validate(MemberValidate::class)->scene('uploadIdCard')->check($param);
        validate(FileValidate::class)->scene('add')->check($param);
        $data = FileService::add($param);
        $result = [
            'file_id'=>$data['file_id'],
            'file_url'=>$data['file_url']
        ];
        //身份证识别
        $idcard_result = AlibabaSdkService::idCardIdentify($param['side'],$data['file_path']);
        if($idcard_result['code']=='400'){
            exception($idcard_result['msg']);
        }else if($idcard_result['code']=='200'){
            $idcard_data = $idcard_result['data'];
            //组装对应参数
            if($idcard_data){
                $field = ['name', 'nationality', 'num', 'sex', 'birth', 'is_fake', 'address', 'start_date', 'end_date', 'issue'];
                foreach ($field as $key=>$val){
                    if(isset($idcard_data[$val]) && $idcard_data[$val]){
                        if($val == 'is_fake' && $param['side']=='face'){
                            $result['is_face_fake'] = $idcard_data[$val]==true?1:0;
                        }else if($val == 'is_fake' && $param['side']=='back'){
                            $result['is_back_fake'] = $idcard_data[$val]==true?1:0;
                        }else{
                            $result[$val] = $idcard_data[$val];
                        }
                    }
                }
            }
        }
        return success($result, '上传成功');
    }
}
